package com.atguigu.gmall.auth.config;

import com.atguigu.gmall.common.utils.RsaUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

@ConfigurationProperties(prefix = "auth.jwt")
@Data
public class JwtProperties {
    private String pubKeyPath;//: D:\rsa\rsa.pub
    private String priKeyPath;//: D:\rsa\rsa.pri
    private String secret;//: 123ewqr@#dfs45
    private Integer expire;//: 180
    private String cookieName;//: GMALL-TOKEN
    private String unick;//: unick

    private PublicKey publicKey;
    private PrivateKey privateKey;
    
    @PostConstruct
    public void init(){
        try {
            File pubFile = new File(pubKeyPath);
            File priFile = new File(priKeyPath);
            // 判断秘钥是否都存在，如果有一个不存在，都要重新生成
            if (!pubFile.exists() || !priFile.exists()) {
                RsaUtils.generateKey(pubKeyPath,priKeyPath,secret);
            }
          this.privateKey=  RsaUtils.getPrivateKey(priKeyPath);
          this.publicKey=  RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
